//
//  RBGVendeurController.m
//  BateauGenerique
//
//  Created by Kevin De Jesus Ferreira on 14/06/2014.
//  Copyright (c) 2014 Kevin De Jesus Ferreira. All rights reserved.
//

#import "RBGVendeurController.h"
#import "RBGNavigationBar.h"
#import "RBGColorManager.h"
#import "RBGAppDelegate.h"
#import "UIImageView+WebCache.h"
#import "Constantes.h"
#import "RBGServiceAnnonce.h"

@interface RBGVendeurController ()<RBGServiceAnnonceDelegate,UIAlertViewDelegate>{
    RBGAppDelegate *appDelegate;
    NSMutableArray * facultatif;
    NSMutableArray * requis;
}

@end

@implementation RBGVendeurController



- (void)viewDidLoad
{
    [super viewDidLoad];
    
    appDelegate = (RBGAppDelegate *)[[UIApplication sharedApplication] delegate];
    
    
    UIBarButtonItem *anotherButton = [[UIBarButtonItem alloc] initWithTitle:@"Effacer" style:UIBarButtonItemStylePlain target:self action:@selector(effacer)];
    self.navigationItem.rightBarButtonItem = anotherButton;
    [self chargerCouleur];
    
    facultatif = [[NSMutableArray alloc]init];
    requis = [[NSMutableArray alloc]init];
    
    
    [facultatif addObject:self.valuePrenom];
    
    [requis addObject:self.valueName];
    [requis addObject:self.valueEmail];
    [requis addObject:self.valueCodePostale];
    [requis addObject:self.valueTelephone];
    [requis addObject:self.valueVille];
}


-(void) effacer
{
    [self effacerValues:requis withValue:@"requis"];
    [self effacerValues:facultatif withValue:@"facultatif"];
}

-(void) effacerValues:(NSArray *) values
            withValue:(NSString *) value
{
    
    for (id valeur in values)
    {
        
        if([valeur isKindOfClass:[UITextField class]]){
            UITextField * tmp = (UITextField *) valeur;
            [tmp setPlaceholder:value];
            [tmp setText:@""];
        }else{
            
            if([valeur isKindOfClass:[UILabel class]]){
                UILabel * tmp = (UILabel *) valeur;
                [tmp setText:value];
                
            }
        }
    }
    
}

-(void) chargerCouleur
{
    [self.fond setImageWithURL:[NSURL URLWithString:appDelegate.donnees.parametres.imageFond]];
    
    [RBGNavigationBar customNavigationBar:self.navigationController andTitle:@"Ma Recherche" andBackgroundColor:appDelegate.donnees.parametres.backgroundColorUn andTextColor:appDelegate.donnees.parametres.fontColorUn];
    
    [RBGColorManager changeTextColorSubViews:self.attributVendeur withColor:appDelegate.donnees.parametres.fontColorDeux];
    
    [self.sendButton setTintColor:appDelegate.donnees.parametres.backgroundColorUn];
    
    
    [self.sendButton setBackgroundColor:appDelegate.donnees.parametres.backgroundColorDeux];
    
    [self.sendButton setTitleColor:appDelegate.donnees.parametres.fontColorDeux forState:UIControlStateNormal];
}

-(void) positionnerVue
{
    
    [self.attributVendeur setFrame:CGRectMake(0, 0, self.attributVendeur.frame.size.width, self.attributVendeur.frame.size.height)];
}

-(void) recupererDonnees{
    
    NSString * nom = [self.valueName text];
    nom = [nom stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    nom = [nom stringByReplacingOccurrencesOfString:@" "
                                         withString:@"%20"];
    
    [self.donnees addEntriesFromDictionary:@{
                                             VENDEUR_NOM:nom
                                             }];
    
    NSString * prenom = [self.valuePrenom text];
    prenom = [prenom stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    prenom = [prenom stringByReplacingOccurrencesOfString:@" "
                                               withString:@"%20"];
    
    [self.donnees addEntriesFromDictionary:@{
                                             VENDEUR_PRENOM:prenom
                                             }];
    
    
    NSString * email = [self.valueEmail text];
    email = [email stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    email = [email stringByReplacingOccurrencesOfString:@" "
                                             withString:@"%20"];
    
    [self.donnees addEntriesFromDictionary:@{
                                             VENDEUR_EMAIL:email
                                             }];
    
    NSString * telephone = [self.valueTelephone text];
    telephone = [telephone stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    telephone = [telephone stringByReplacingOccurrencesOfString:@" "
                                                     withString:@"%20"];
    
    [self.donnees addEntriesFromDictionary:@{
                                             VENDEUR_TEL_1:telephone
                                             }];
    
    NSString * codePostal = [self.valueCodePostale text];
    codePostal = [codePostal stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    codePostal = [codePostal stringByReplacingOccurrencesOfString:@" "
                                                       withString:@"%20"];
    
    [self.donnees addEntriesFromDictionary:@{
                                             VENDEUR_CODE_POSTAL:codePostal
                                             }];
    
    NSString * ville = [self.valueVille text];
    ville = [ville stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    ville = [ville stringByReplacingOccurrencesOfString:@" "
                                             withString:@"%20"];
    
    [self.donnees addEntriesFromDictionary:@{
                                             VENDEUR_VILLE:ville
                                             }];
    
}

- (IBAction)valider:(id)sender {
    
    
    if([[self.valueName text] length] == 0){
        [self afficherAlerte:@"Veuillez indiquer votre nom"];
    }else{
        if([[self.valueEmail text] length] == 0){
            [self afficherAlerte:@"Veuillez indiquer votre email"];
        }else{
            if([[self.valueCodePostale text] length] == 0){
                [self afficherAlerte:@"Veuillez indiquer votre code postal"];
            }else{
                if([[self.valueTelephone text] length] == 0){
                    [self afficherAlerte:@"Veuillez indiquer votre numéro de téléphone"];
                }else{
                    if([[self.valueVille text] length] == 0){
                        [self afficherAlerte:@"Veuillez indiquer votre ville"];
                    }else{
                        [self validerVendeur];
                    }
                }
            }
        }
    }
}

-(void) validerVendeur {
    [self recupererDonnees];
    
[UIApplication sharedApplication].networkActivityIndicatorVisible = TRUE;
    RBGServiceAnnonce * service = [[RBGServiceAnnonce alloc]init];
    service.delegate = self;
    [service onDemand:self.donnees];
}

-(void) afficherAlerte:(NSString *)mes
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Erreur Champs"
                                                    message:mes
                                                   delegate:nil
                                          cancelButtonTitle:@"OK"
                                          otherButtonTitles:nil];
    [alert show];
}


- (void) receiveDonneesAnnonce:(id)donnees
                      andType :(int)typeDonnees{
    
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Recherche"
                                                    message:@"Votre recherche a bien été prise en compte"
                                                   delegate:self
                                          cancelButtonTitle:@"OK"
                                          otherButtonTitles:nil];
    [alert show];
    
}

- (void) receiveErreurAnnonce:(int)statut
{
    UIAlertView *alert = [[UIAlertView alloc]
                          
                          initWithTitle:@"Erreur"
                          message:@"Erreur réseau : votre téléphone ne peut pas accéder aux informations"
                          delegate:self
                          cancelButtonTitle:@"Ok"
                          otherButtonTitles:nil];
    
    [alert show];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{

    [self effacerValues:requis withValue:@"requis"];
    [self effacerValues:facultatif withValue:@"facultatif"];
    [self.delegate effacerDonnees];

    [self.navigationController popToRootViewControllerAnimated:YES];

}



@end
